package com.jajahome.feature.user.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jajahome.R;
import com.jajahome.base.BaseListHomeAdapter;
import com.jajahome.constant.Constant;
import com.jajahome.feature.user.activity.ChipsAct;
import com.jajahome.feature.user.activity.CrowDetailAct;
import com.jajahome.model.CrowModel;
import com.jajahome.util.DensityUtil;
import com.jajahome.util.StringUtil;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by tuxuesong on 2018/5/14.
 */

public class CrowItemAdapter extends BaseListHomeAdapter<CrowModel.DataEntity.CrowdListEntity> {

    private int w;

    public CrowItemAdapter(Context context, List<CrowModel.DataEntity.CrowdListEntity> list) {
        super(context, list);
        w = (int) DensityUtil.getDisplayWidthDp(context);
    }


    @Override
    public View initView(final CrowModel.DataEntity.CrowdListEntity crowdListEntity, View convertView, final ViewGroup parent, int position) {
        CrowItemAdapter.ViewHolder holder;
        if (convertView == null) {
            LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
            convertView = mInflater.inflate(R.layout.act_crow_item_item, null);
            holder = new CrowItemAdapter.ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.name);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.price);
            holder.img = (ImageView) convertView.findViewById(R.id.left_img);
            holder.tvSupport = (TextView) convertView.findViewById(R.id.support);
            holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress);
            holder.container = (RelativeLayout) convertView.findViewById(R.id.container);
            holder.tvPercent = (TextView) convertView.findViewById(R.id.percent);
            holder.supportProject = (TextView) convertView.findViewById(R.id.supportProject);
            holder.img_user = (ImageView) convertView.findViewById(R.id.act_crow_user);
            convertView.setTag(holder);
        } else {
            holder = (CrowItemAdapter.ViewHolder) convertView.getTag();
        }
        com.zhy.android.percent.support.PercentLinearLayout.LayoutParams params = (com.zhy.android.percent.support.PercentLinearLayout.LayoutParams) holder.progressBar.getLayoutParams();
        params.width = (int) (w * 0.6);
        holder.progressBar.setLayoutParams(params);

        String crowName = crowdListEntity.getName();
        if (!StringUtil.isEmpty(crowName)) {
            holder.tvName.setText(crowName);
        }
        String crowTarAmount = crowdListEntity.getTarAmount();
        String crowAmRaised = crowdListEntity.getAmRaised();
        //已筹金额
        if (!StringUtil.isEmpty(crowAmRaised)) {
            holder.tvPrice.setText(Constant.YUAN + StringUtil.spilStr(crowAmRaised));
        }

//        String crowAmRaised = crowdListEntity.getAmRaised();
        if (!StringUtil.isEmpty(crowTarAmount) && !StringUtil.isEmpty(crowAmRaised)) {
            holder.progressBar.setMax(Integer.parseInt(StringUtil.spilStr(crowTarAmount)));
            holder.progressBar.setProgress(Integer.parseInt(StringUtil.spilStr(crowAmRaised)));
            //大于999只显示>999%
            if (Float.parseFloat(crowAmRaised) / Float.parseFloat(crowTarAmount) * 100>=999){
                holder.tvPercent.setText("＞999%");
            }else {
                holder.tvPercent.setText(StringUtil.spilStr(((Float.parseFloat(crowAmRaised) / Float.parseFloat(crowTarAmount)) * 100) + "") + "%");
            }
        }

        String crowState = crowdListEntity.getState();
        String crowDesc = crowdListEntity.getDesc();
        String crowBuyCount = crowdListEntity.getBuy_count();
        if (crowState.equals("1")) {
            holder.supportProject.setText("支持项目");
            holder.tvSupport.setText(crowBuyCount + "人已支持");
            holder.img_user.setVisibility(View.VISIBLE);
            holder.supportProject.setTextColor(context.getResources().getColor(R.color.orange_ll));
            holder.supportProject.setBackgroundResource(R.drawable.bg_circle_orign);
        } else if (crowState.equals("2") || crowState.equals("4")) {
            holder.supportProject.setText("了解项目");
            holder.tvSupport.setText("众筹成功");
            holder.img_user.setVisibility(View.GONE);
            holder.supportProject.setTextColor(context.getResources().getColor(R.color.text_black_server));
            holder.supportProject.setBackgroundResource(R.drawable.bg_circle_orign1);
        } else if (crowState.equals("3") || crowState.equals("5")) {
            holder.supportProject.setText("了解项目");
            holder.tvSupport.setText("众筹失败");
            holder.img_user.setVisibility(View.GONE);
            holder.supportProject.setTextColor(context.getResources().getColor(R.color.text_black_server));
            holder.supportProject.setBackgroundResource(R.drawable.bg_circle_orign1);
        }

        String crowUrl = crowdListEntity.getImage().getThumb();
        if (!StringUtil.isEmpty(crowUrl)) {
            Picasso.with(parent.getContext()).load(crowUrl).placeholder(R.mipmap.newlogo_square).into(holder.img);
        }


        holder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(parent.getContext(), CrowDetailAct.class);
                intent.putExtra(CrowDetailAct.CROW_ID, crowdListEntity.getId());
                intent.putExtra(CrowDetailAct.AMRAISED, crowdListEntity.getAmRaised());
                intent.putExtra(CrowDetailAct.TARAMOUNT, crowdListEntity.getTarAmount());
                intent.putExtra(CrowDetailAct.MEMO, crowdListEntity.getMemo());
                intent.putExtra(CrowDetailAct.TYPE, crowdListEntity.getState());
                intent.putExtra(ChipsAct.POSTAGE, crowdListEntity.getPostage());
                intent.putExtra(ChipsAct.BUYCOUNT, crowdListEntity.getBuy_count());
                parent.getContext().startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView tvName, tvPercent, tvPrice, tvSupport, supportProject;
        RelativeLayout container;
        ProgressBar progressBar;
        ImageView img,img_user;
    }

}
